package cc.mcggdd.leah.boot.stock.service.impl;

import cc.mcggdd.leah.boot.core.security.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.mcggdd.leah.boot.stock.mapper.StockHoldingMapper;
import cc.mcggdd.leah.boot.stock.service.StockHoldingService;
import cc.mcggdd.leah.boot.stock.model.entity.StockHolding;
import cc.mcggdd.leah.boot.stock.model.form.StockHoldingForm;
import cc.mcggdd.leah.boot.stock.model.query.StockHoldingQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockHoldingVO;
import cc.mcggdd.leah.boot.stock.converter.StockHoldingConverter;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 持仓管理服务实现类
 *
 * @author 
 * @since 2025-02-13 09:38
 */
@Service
@RequiredArgsConstructor
public class StockHoldingServiceImpl extends ServiceImpl<StockHoldingMapper, StockHolding> implements StockHoldingService {

    private final StockHoldingConverter stockHoldingConverter;

    /**
    * 获取持仓管理分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<StockHoldingVO>} 持仓管理分页列表
    */
    @Override
    public IPage<StockHoldingVO> getStockHoldingPage(StockHoldingQuery queryParams) {
        Page<StockHoldingVO> pageVO = this.baseMapper.getStockHoldingPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取持仓管理表单数据
     *
     * @param id 持仓管理ID
     * @return
     */
    @Override
    public StockHoldingForm getStockHoldingFormData(Long id) {
        StockHolding entity = this.getById(id);
        return stockHoldingConverter.toForm(entity);
    }
    
    /**
     * 新增持仓管理
     *
     * @param formData 持仓管理表单对象
     * @return
     */
    @Override
    public Long saveStockHolding(StockHoldingForm formData) {
        StockHolding entity = stockHoldingConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserId());
        if (!this.save(entity))
            return 0l;
        else return entity.getId();
    }
    
    /**
     * 更新持仓管理
     *
     * @param id   持仓管理ID
     * @param formData 持仓管理表单对象
     * @return
     */
    @Override
    public boolean updateStockHolding(Long id,StockHoldingForm formData) {
        StockHolding entity = stockHoldingConverter.toEntity(formData);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }
    
    /**
     * 删除持仓管理
     *
     * @param ids 持仓管理ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteStockHoldings(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的持仓管理数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
